<!--
 @description 任务分页
 @fileName List.vue
 @author zengqiongying
 @created 2023/04/13 13:56:50
-->
<template>
  <div class="task-page-list">
    <List ref="qmpList" v-model:finished="finished" :reset-fun="resetData" :load="load" :refresh="load">
      <Item
        v-for="(item, i) in list"
        :key="`${i}-${item.caid}`"
        :item-data="item"
        :pageSource="pageSource"
        @detail="toDetail(item)"
        @return="onReturnBack(item)"
        @edit="onEdit(item)"
        @review="onReview(item)"
      />
    </List>
  </div>
</template>

<script lang="ts">
  import { ref, reactive, toRefs, watch, defineComponent, getCurrentInstance } from 'vue'
  import { useRouter } from 'vue-router'
  import List from '@/components/List/index.vue'
  import Item from './Item.vue'
  import { queryTaskPage, queryTaskDetail } from '@/apis/Task'
  import { TaskReturn } from '@/mixins/TaskCommon'
  import { IAnyKeyObject } from '@/types/common'
  export default defineComponent({
    name: 'TaskCommonList',
    components: { List, Item },
    props: {
      pageSource: {
        type: String,
        default: '' //页面来源（task 任务管理，soft 软装，hard 硬装 , todoList 首页待办）
      },
      searchValue: {
        type: String,
        default: ''
      },
      status: {
        type: String,
        default: '' // 任务状态：1 未派发， 2 验收中 ， 3 已提交， 4 已完成
      }
    },
    emits: ['review'],
    setup(props, { emit }) {
      const { proxy }: any = getCurrentInstance()
      const $router = useRouter()
      const qmpList = ref()
      const datas = reactive<IAnyKeyObject>({
        list: [],
        finished: false,
        showReview: false,
        activeRowData: ''
      })
      const statusArray = ['', '未派发', '进行中', '已提交', '已完成']

      const load = async (page: number, rows: number) => {
        let searchWorld = props.searchValue || ''
        let status = statusArray[Number(props.status || 0)] || ''
        if (props.pageSource === 'todoList') {
          status = '进行中'
        }
        const reportMold = props.pageSource === 'soft' ? '软装' : props.pageSource === 'hard' ? '硬装' : ''
        const paramsData = {
          page,
          rows,
          status,
          searchWorld,
          reportMold,
          isTreat: props.pageSource === 'todoList' ? 1 : 0,
          pageFlag: true
        }
        return queryTaskPage(paramsData).then((res: any) => {
          const resData = res.data
          const list = resData.records
          if (page === 1) {
            datas.list = list
          } else {
            datas.list = datas.list.concat(list)
          }
          if (resData.total <= datas.list.length) {
            datas.finished = true
          }
        })
      }
      const resetData = () => {
        datas.list = []
      }
      const resetPageData = () => {
        qmpList.value.resetPageData()
      }

      const refreshItem = (caid: string) => {
        const flag = datas.list.some((v: any) => v.caid === caid)
        if (!flag) return

        queryTaskDetail(caid).then((res) => {
          const d = res.data
          if (d && d.id) {
            datas.list = datas.list.map((item: any) => {
              if (item.caid === caid) {
                item = { ...res.data }
              }
              return item
            })
          }
        })
      }

      watch(
        () => props.searchValue,
        () => {
          resetPageData()
        }
      )

      const toDetail = (row: any) => {
        $router.push({
          path: `/task/detail`,
          query: {
            source: props.pageSource,
            id: row.caid
          }
        })
      }

      const onReturnBack = (row: any) => {
        const { caid }: any = row
        TaskReturn(caid, () => {
          /* if (props.status === 'saved') {
            datas.list = datas.list.filter((v: any) => v.caid !== caid)
          } else {
            refreshItem(caid)
          } */
        })
      }

      const onEdit = (row: any) => {
        $router.push({
          path: `/task/edit`,
          query: {
            source: props.pageSource,
            id: row.caid
          }
        })
      }

      const onReview = (row: any) => {
        // 复查确认
        emit('review', row)
      }

      return {
        ...toRefs(datas),
        load,
        resetData,
        qmpList,
        resetPageData,
        refreshItem,
        onEdit,
        toDetail,
        onReturnBack,
        onReview
      }
    }
  })
</script>
<style lang="less" scoped>
  .task-page-list {
    position: relative;
    padding: 0 14px;
  }
</style>
